package com.waypolice.common.pdfUtil;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.waypolice.common.utils.PropertiesFileUtil;
import com.waypolice.pojo.CmsJudItem;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangrang
 * @create 2018-04-18 17:53
 * @file name   ProducePdf
 * @desc 生成pdf工具类
 **/
public class ProducePdf {
    /**
     * @method producePdf
     * @description
     * @param size  列的数量
    * @param titleFile 列表标题
    * @param columnsHeadList   列头list集合
    * @param listHashMap   列头对应数据list集合
     * @return java.lang.String
     * @date 2018/4/20 8:40
     */
    public static byte[] producePdf(String path,int size,String titleFile,List<String> columnsHeadList, Map<String, List<Object>> listHashMap) throws  IOException, DocumentException {

        // 1.新建document对象
        Document document = new Document(PageSize.A4, 10, 10, 50, 50);
        // 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
        // 获取生成本地pdf文件的地址，上传后会自动删除
        //项目临时文件夹itemCatalogPDF.pdf
        String str=path;

        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        //添加页码
        writer.setPageEvent(new PdfPageHelper());

        // 3.打开文档
        document.open();
        // 中文字体,解决中文不能显示问题
        BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        // 主标题黑色字体
        Font titleFont = new Font(bfChinese, 14, Font.NORMAL);
        // 第二级标题字体
        Font secondTitleChinese = new Font(bfChinese, 16, Font.BOLD);

        // 内容字体
        Font contentTitleChinese = new Font(bfChinese, 11, Font.NORMAL);

        // 创建章节 titleFile文件标题
        Paragraph title = new Paragraph(titleFile, titleFont);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);

        Paragraph secondTitle = new Paragraph("                       ",
                secondTitleChinese);
        secondTitle.setSpacingBefore(5f);
        secondTitle.setAlignment(Element.ALIGN_CENTER);
        document.add(secondTitle);

        // 设置表格的列宽和列数
        int size2 = columnsHeadList.size();
        //float[] widths = { 8f, 15f, 5f, 15f, 35f, 40f, 20f, 35f };
        float[] widths = new float[size2];
        for (int i = 0; i < widths.length; i++) {
            widths[i] = 15f;
        }

        // 建立一个pdf表格
        PdfPTable table = new PdfPTable(widths);

        // 设置段落间距
        table.setSpacingBefore(5f);

        // 实现单元格跨页显示
        table.setSplitLate(false);
        table.setSplitRows(true);


        // 设置表格宽度为100%
        table.setWidthPercentage(90);
        PdfPCell cell = null;
        // 第一行 列头
        for (String columnsHead : columnsHeadList) {
            String[] split = columnsHead.split("\\|");

            cell = new PdfPCell(new Paragraph(split[2], contentTitleChinese));
            cell.setMinimumHeight(20F);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
        }

        //第二行至结束，填充数据，列头匹配
        for (int i = 0; i < size; i++) {
            for (String columnsHeadData : columnsHeadList) {
                String[] splitData = columnsHeadData.split("\\|");
                String splitDatum = splitData[0];
                List<Object> objects = listHashMap.get(splitDatum);
                Object o = objects.get(i);
                String svalue = JudgmentObjectAttr.JudementAttr(o);

                cell = new PdfPCell(new Paragraph(svalue, contentTitleChinese));
                cell.setMinimumHeight(20F);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

            }

        }
        //将表格写入document文档中
        document.add(table);

        // 5.关闭文档
        if(document !=null){
            document.close();
        }
        writer.close();
        byte[] bytes = fos.toByteArray();
        //String result =new String(fos.toByteArray());//转字符串设置编码
       // result = java.net.URLEncoder.encode(result);//如果跨域需设置编码

        return bytes;
    }

    /**
     * @method setBorderWidth
     * @description 设置边线的宽度
     * @param cell
    * @param top
    * @param buttom
    * @param left
    * @param right
     * @return void
     * @date 2018/4/19 19:26
     */
    private static void setBorderWidth(PdfPCell cell,float top,float buttom,float left,float right){
        if(top==0){
            cell.setBorderWidthTop(top);
        }
        if(buttom==0){
            cell.setBorderWidthBottom(buttom);
        }
        if(left==0){
            cell.setBorderWidthLeft(left);
        }
        if(right==0){
            cell.setBorderWidthRight(right);
        }
    }


    /**
     * 获取oldPath项目路径
     * @return
     */
    public static String projectWay(){
        StringBuffer sb  = new StringBuffer();
        sb.append(PropertiesFileUtil.class.getResource("/").getPath());
        sb.append("/comlocalpdf/");
        return sb.toString();
    }

    /**
     * 判断文件是否存在
     * @return 0:文件不存在，1:文件存在无内容，2：文件存在内容存在
     */
    public static int fileExists(String path){
        File file = new File(path);

        if(!file.exists()){
            return 0;
        }else{
            if (file.length() != 0){
                return 2;
            }else{
                return 1;
            }
        }
    }

    /**
     * 生成固定模板的卷内目录pdf
     * @param headMap 卷头
     * @param cmsJud 卷内件目录
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static byte[] producePdfFixed(HashMap<String, String> headMap,List<CmsJudItem> cmsJud) throws  IOException, DocumentException {

        // 1.新建document对象
        Document document = new Document(PageSize.A4, 10, 10, 50, 50);
        // 2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
        // 创建 PdfWriter 对象 第一个参数是对文档对象的引用，第二个参数是文件的实际名称，在该名称中还会给出其输出路径。
        // 获取生成本地pdf文件的地址，上传后会自动删除
        //项目临时文件夹itemCatalogPDF.pdf
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        //添加页码
        writer.setPageEvent(new PdfPageHelper());

        // 3.打开文档
        document.open();
        // 中文字体,解决中文不能显示问题
        BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 主标题黑色字体
        Font titleFont = new Font(bfChinese, 18, Font.NORMAL);
        // 第二级标题字体
        Font secondTitleChinese = new Font(bfChinese, 16, Font.BOLD);
        // 内容字体
        Font contentTitleChinese = new Font(bfChinese, 11, Font.NORMAL);
        // 创建章节 titleFile文件标题
        Paragraph title = new Paragraph(headMap.get("volClassName"), titleFont);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);

        float[] widths = { 30f, 40f, 30f, 80f, 30f, 20f, 20f };
        // 建立一个pdf表格
        PdfPTable table = new PdfPTable(7);
        // 设置表格宽度比例为%100
        table.setWidthPercentage(90);
        // 设置段落间距
        table.setSpacingBefore(5f);
        // 也可以每列分别设置宽度
        table.setTotalWidth(widths);
        // 实现单元格跨页显示
        table.setSplitLate(false);
        table.setSplitRows(true);

        PdfPCell firstRow1 = new PdfPCell(new Paragraph("案  卷  题  名", contentTitleChinese));
        firstRow1.setColspan(2); // 设置跨两列
        firstRow1.setMinimumHeight(20F);
        firstRow1.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置内容水平居中显示
        firstRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);// 设置垂直居中
        table.addCell(firstRow1);

        PdfPCell firstRow2 = new PdfPCell(new Paragraph(headMap.get("volTitle"), contentTitleChinese));
        firstRow2.setColspan(5);
        firstRow2.setMinimumHeight(20F);
        firstRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
        firstRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(firstRow2);

        PdfPCell secondRow1 = new PdfPCell(new Paragraph("归档年度", contentTitleChinese));
        secondRow1.setMinimumHeight(20F);
        secondRow1.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow1);

        PdfPCell secondRow2 = new PdfPCell(new Paragraph(headMap.get("volArchiveYear"), contentTitleChinese));
        secondRow2.setMinimumHeight(20F);
        secondRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow2);

        PdfPCell secondRow3 = new PdfPCell(new Paragraph("审计组长", contentTitleChinese));
        secondRow3.setMinimumHeight(20F);
        secondRow3.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow3);

        PdfPCell secondRow4 = new PdfPCell(new Paragraph(headMap.get("volAuditLeader"), contentTitleChinese));
        secondRow4.setMinimumHeight(20F);
        secondRow4.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow4);

        PdfPCell secondRow5 = new PdfPCell(new Paragraph("保管期限", contentTitleChinese));
        secondRow5.setMinimumHeight(20F);
        secondRow5.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow5);

        PdfPCell secondRow6 = new PdfPCell(new Paragraph(headMap.get("volStorageTime"), contentTitleChinese));
        secondRow6.setColspan(2);
        secondRow6.setMinimumHeight(20F);
        secondRow6.setHorizontalAlignment(Element.ALIGN_CENTER);
        secondRow6.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(secondRow6);

        PdfPCell thirdRow1 = new PdfPCell(new Paragraph("归档单位", contentTitleChinese));
        thirdRow1.setMinimumHeight(20F);
        thirdRow1.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow1);

        PdfPCell thirdRow2 = new PdfPCell(new Paragraph(headMap.get("volArchiveUnit"), contentTitleChinese));
        thirdRow2.setMinimumHeight(20F);
        thirdRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow2);

        PdfPCell thirdRow3 = new PdfPCell(new Paragraph("审计时间", contentTitleChinese));
        thirdRow3.setMinimumHeight(20F);
        thirdRow3.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow3);

        PdfPCell thirdRow4 = new PdfPCell(new Paragraph(headMap.get("volAuditDate"), contentTitleChinese));
        thirdRow4.setMinimumHeight(20F);
        thirdRow4.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow4);

        PdfPCell thirdRow5 = new PdfPCell(new Paragraph("案卷号", contentTitleChinese));
        thirdRow5.setMinimumHeight(20F);
        thirdRow5.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow5);

        PdfPCell thirdRow6 = new PdfPCell(new Paragraph(headMap.get("volNo"), contentTitleChinese));
        thirdRow6.setColspan(2);
        thirdRow6.setMinimumHeight(20F);
        thirdRow6.setHorizontalAlignment(Element.ALIGN_CENTER);
        thirdRow6.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(thirdRow6);

        PdfPCell fourthRow1 = new PdfPCell(new Paragraph(headMap.get("volTotal"), contentTitleChinese));
        fourthRow1.setMinimumHeight(20F);
        fourthRow1.setColspan(3);
        fourthRow1.setHorizontalAlignment(Element.ALIGN_CENTER);
        fourthRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fourthRow1);

        PdfPCell fourthRow2 = new PdfPCell(new Paragraph(headMap.get("volCurrent"), contentTitleChinese));
        fourthRow2.setMinimumHeight(20F);
        fourthRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
        fourthRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fourthRow2);

        PdfPCell fourthRow3 = new PdfPCell(new Paragraph("密  级", contentTitleChinese));
        fourthRow3.setMinimumHeight(20F);
        fourthRow3.setHorizontalAlignment(Element.ALIGN_CENTER);
        fourthRow3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fourthRow3);

        PdfPCell fourthRow4 = new PdfPCell(new Paragraph(headMap.get("volSecret"), contentTitleChinese));
        fourthRow4.setColspan(2);
        fourthRow4.setMinimumHeight(20F);
        fourthRow4.setHorizontalAlignment(Element.ALIGN_CENTER);
        fourthRow4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fourthRow4);

        PdfPCell fifthRow1 = new PdfPCell(new Paragraph("序号", contentTitleChinese));
        fifthRow1.setMinimumHeight(20F);
        fifthRow1.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow1);

        PdfPCell fifthRow2 = new PdfPCell(new Paragraph("文号", contentTitleChinese));
        fifthRow2.setMinimumHeight(20F);
        fifthRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow2);

        PdfPCell fifthRow3 = new PdfPCell(new Paragraph("责任者", contentTitleChinese));
        fifthRow3.setMinimumHeight(20F);
        fifthRow3.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow3);

        PdfPCell fifthRow4 = new PdfPCell(new Paragraph("文  件  标  题", contentTitleChinese));
        fifthRow4.setMinimumHeight(20F);
        fifthRow4.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow4);

        PdfPCell fifthRow5 = new PdfPCell(new Paragraph("发文日期", contentTitleChinese));
        fifthRow5.setMinimumHeight(20F);
        fifthRow5.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow5);

        PdfPCell fifthRow6 = new PdfPCell(new Paragraph("页 号", contentTitleChinese));
        fifthRow6.setMinimumHeight(20F);
        fifthRow6.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow6.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow6);

        PdfPCell fifthRow7 = new PdfPCell(new Paragraph("密 级", contentTitleChinese));
        fifthRow7.setMinimumHeight(20F);
        fifthRow7.setHorizontalAlignment(Element.ALIGN_CENTER);
        fifthRow7.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(fifthRow7);

        PdfPCell sixthRow=null;
        for (CmsJudItem cmsJudItem : cmsJud) {
            //序号
            String itemNum = cmsJudItem.getItemNum() == null ? "" : cmsJudItem.getItemNum().toString();
            PdfPCell sixthRow1 = new PdfPCell(new Paragraph(itemNum, contentTitleChinese));
            sixthRow1.setMinimumHeight(20F);
            sixthRow1.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow1);
            //文号
            PdfPCell sixthRow2 = new PdfPCell(new Paragraph(cmsJudItem.getItemName(), contentTitleChinese));
            sixthRow2.setMinimumHeight(20F);
            sixthRow2.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow2);
            //责任者
            PdfPCell sixthRow3 = new PdfPCell(new Paragraph(cmsJudItem.getItemRespons(), contentTitleChinese));
            sixthRow3.setMinimumHeight(20F);
            sixthRow3.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow3);
            //文件标题
            PdfPCell sixthRow4 = new PdfPCell(new Paragraph(cmsJudItem.getItemTitle(), contentTitleChinese));
            sixthRow4.setMinimumHeight(20F);
            sixthRow4.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow4);
            //发文日期
            PdfPCell sixthRow5 = new PdfPCell(new Paragraph(cmsJudItem.getItemCreateTime(), contentTitleChinese));
            sixthRow5.setMinimumHeight(20F);
            sixthRow5.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow5);
            //页号
            PdfPCell sixthRow6 = new PdfPCell(new Paragraph(cmsJudItem.getItemPageNo(), contentTitleChinese));
            sixthRow6.setMinimumHeight(20F);
            sixthRow6.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow6.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow6);
            //密级
            PdfPCell sixthRow7 = new PdfPCell(new Paragraph(cmsJudItem.getItemSecret(), contentTitleChinese));
            sixthRow7.setMinimumHeight(20F);
            sixthRow7.setHorizontalAlignment(Element.ALIGN_CENTER);
            sixthRow7.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(sixthRow7);
        }

        //将表格写入document文档中
        document.add(table);
        // 5.关闭文档
        if(document !=null){
            document.close();
        }
        writer.close();
        byte[] bytes = fos.toByteArray();
        //String result =new String(fos.toByteArray());//转字符串设置编码
        // result = java.net.URLEncoder.encode(result);//如果跨域需设置编码

        return bytes;
    }

}
